package JdbcTemplate.config;

import JdbcTemplate.operation.query.Person;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class Config {

    /*
    注入BasicDataSource数据源
     */
    //@Bean
    public BasicDataSource dataSource(){
        BasicDataSource source=new BasicDataSource();
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3306/spring");
        source.setPassword("root");
        source.setUsername("root");
        return source;
    }

    /*
    注入模板
    */
    //@Bean
    public JdbcTemplate jdbcTemplate(BasicDataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    /*
    面向接口
     */
    @Bean
    public JdbcOperations jdbcOperations(){
        return new JdbcTemplate(dataSource());
    }

    /*
    注入Person对象
     */
    @Bean
    public Person getPerson(){
        return new Person();
    }
}
